<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>点击发送跨域请求</button>
    <div id="result">

    </div>
    <script>
        // 1. 获取按钮和result
        let btn = document.querySelector('button');
        let div = document.querySelector('#result')
            // 2. 定义一个命名函数，设置形参
        function b(data) {
            // 2.1 将参数数据放入到result标签内
            div.innerHTML = data.name;
        }

        // 3. 绑定点击事件
        btn.onclick = function() {
            jsonp('http://localhost/jsonpdemo', 'b');
        }

        function jsonp(url, cb) {
            // 3.1 创建script标签
            let script = document.createElement('script');
            // 3.2 输入script的src路径
            // http://localhost/jsonpdemo?cb=b'
            script.src = `${url}?cb=${cb}`;
            // 3.3 将script插入到body中
            document.body.appendChild(script);
        }

        // b({name:"laowang",age:18})
    </script>

</body>

</html>